/*
 * Copyright (c) 2020 HiHope Community.
 * Description:  as312 serson get data from adc change
 * Author: HiSpark Product Team.
 * Create: 2020-5-20
 */
#include <hi_watchdog.h>
#include <hi_early_debug.h>
#include <hi_time.h>
#include <hi_io.h>
#include <hi_gpio.h>
#include <hi_adc.h>
#include <hi_pwm.h>
#include <hi_sem.h>
#include <hi_stdlib.h>
#include <hi_task.h>

#define     ADC_TEST_LENGTH             (20)
#define     VLT_MIN                     (100)
#define     OLED_FALG_ON                ((hi_u8)0x01)
#define     OLED_FALG_OFF               ((hi_u8)0x00)

hi_u16  g_as312_adc_buf[ADC_TEST_LENGTH] = { 0 };

/*get IR sensor data:IR serson test*/
hi_u8 get_human_status(hi_void)
{
    hi_u16 data;
    hi_u32 ret;
    hi_u32 i;
    hi_u16 vlt;
    float voltage;
    float vlt_max = 0;
    float vlt_min = VLT_MIN;

    for (int i = 0; i < ADC_TEST_LENGTH; i++) {
        ret = hi_adc_read(HI_ADC_CHANNEL_0, &data, HI_ADC_EQU_MODEL_4, HI_ADC_CUR_BAIS_DEFAULT, 0xF0);//ADC_Channal_6  自动识别模式  CNcomment:4次平均算法模式 CNend */
        if (ret != HI_ERR_SUCCESS) 
        {
            return  HI_NULL;
        }
        g_as312_adc_buf[i] = data;
    }

    for (i = 0; i < ADC_TEST_LENGTH; i++) {  
        vlt = g_as312_adc_buf[i]; 
        voltage = (float)vlt * 1.8 * 4 / 4096.0;  /* vlt * 1.8 * 4 / 4096.0为将码字转换为电压 */
        vlt_max = (voltage > vlt_max) ? voltage : vlt_max;
        vlt_min = (voltage < vlt_min) ? voltage : vlt_min;
    }

    if(vlt_max <3.0) {
        return OLED_FALG_OFF;
    } else {
        return OLED_FALG_ON;
    }
}
